﻿/**
 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 */

'use strict';

(function () {
    CKEDITOR.dialog.add('OfficeUpload', function (editor) {

        var commonLang = editor.lang.common,
			linkLang = editor.lang.link,
			anchors;

        return {
            title: "文档上传",
            minWidth: 350,
            minHeight: 230,
            contents: [{
                id: 'info',
                label: editor.lang.common.generalTab,
                accessKey: 'I',
                elements: [
                    {
                        type: 'vbox',
                        padding: 0,
                        children: [{
                            type: 'hbox',
                            widths: ['260px', '110px'],
                            align: 'right',
                            children: [{
                                id: 'title',
                                type: 'text',
                                label: "标题",
                                required: true,
                                validate: CKEDITOR.dialog.validate.notEmpty('标题不能为空'),
                                setup: function (element) {
                                    this.setValue(element.getText());
                                },

                                commit: function (element) {
                                    element.setText(this.getValue());
                                }
                            }]
                        }]
                    },
                    {
                        type: 'vbox',
                        padding: 0,
                        children: [{
                            type: 'hbox',
                            widths: ['250px', '110px'],
                            align: 'right',
                            children: [{
                                id: 'src',
                                type: 'text',
                                label: "文档地址",
                                setup: function (element) {
                                    this.setValue(element.getAttribute("data"));
                                },

                                commit: function (element) {
                                    element.setAttribute("data", this.getValue());
                                }
                            },
                            {
                                id: 'transfer',
                                type: 'button',
                                label: "转换为pdf",
                                style: 'display:inline-block;margin-top:14px;',
                                onClick: function () {
                                    var dialog = this.getDialog();
                                    var filePath = dialog.getValueOf('info', 'src');
                                    if (filePath != '') {
                                        $.ajax({
                                            url: '/admin/transfer/topdf',
                                            type: 'post',
                                            async: false,
                                            data: {
                                                file: filePath
                                            },
                                            success: function (d) {
                                                if (d.IsSuccess) {
                                                    dialog.setValueOf('info', 'pdfPath', d.Data);
                                                } else {
                                                    alert(d.Message);
                                                }
                                            },
                                            error: function (res) {
                                                console.log(res);
                                            }
                                        });
                                    }
                                }
                            },
                            {
                                type: 'button',
                                id: 'browse',
                                hidden: true,
                                // v-align with the 'src' field.
                                // TODO: We need something better than a fixed size here.
                                style: 'display:inline-block;margin-top:14px;',
                                label: editor.lang.common.browseServer
                            }]
                        }]
                    },
                    {
                        type: 'vbox',
                        padding: 0,
                        children: [{
                            type: 'hbox',
                            widths: ['260px', '110px'],
                            align: 'right',
                            children: [{
                                id: 'pdfPath',
                                type: 'text',
                                label: "pdf地址",
                                required: true,
                                validate: CKEDITOR.dialog.validate.notEmpty('pdf地址不能为空'),
                                setup: function (element) {
                                    this.setValue(element.getAttribute("data-pdf"));
                                },

                                commit: function (element) {
                                    element.setAttribute("data-pdf", this.getValue());
                                    element.setAttribute("src", "/Content/pdf/web/viewer.html?file=" + this.getValue());
                                }
                            }]
                        }]
                    },
                    {
                        type: 'hbox',
                        widths: ['25%', '25%', '25%', '25%', '25%'],
                        children: [{
                            type: 'text',
                            id: 'width',
                            requiredContent: 'iframe[width]',
                            style: 'width:95px',
                            label: editor.lang.common.width,
                            validate: CKEDITOR.dialog.validate.htmlLength(editor.lang.common.invalidHtmlLength.replace('%1', editor.lang.common.width)),
                            setup: function (element) {
                                this.setValue(element.getAttribute("width"));
                            },

                            commit: function (element) {
                                var val = this.getValue();
                                if (val) {
                                    element.setAttribute("width", this.getValue());
                                } else {
                                    element.removeAttribute("width");
                                }
                            }
                        },
                        {
                            type: 'text',
                            id: 'height',
                            requiredContent: 'iframe[height]',
                            style: 'width:95px',
                            label: editor.lang.common.height,
                            validate: CKEDITOR.dialog.validate.htmlLength(editor.lang.common.invalidHtmlLength.replace('%1', editor.lang.common.height)),
                            setup: function (element) {
                                this.setValue(element.getAttribute("height"));
                            },

                            commit: function (element) {
                                var val = this.getValue();
                                if (val) {
                                    element.setAttribute("height", this.getValue());
                                } else {
                                    element.removeAttribute("height");
                                }
                            }
                        }]
                    }
                ]
            },
            {
                id: 'upload',
                label: linkLang.upload,
                title: linkLang.upload,
                hidden: true,
                filebrowser: 'uploadButton',
                elements: [{
                    type: 'file',
                    id: 'upload',
                    label: commonLang.upload,
                    style: 'height:40px',
                    size: 29
                },
				{
				    type: 'fileButton',
				    id: 'uploadButton',
				    label: commonLang.uploadSubmit,
				    filebrowser: 'info:src',
				    'for': ['upload', 'upload']
				}]
            }],
            onShow: function () {
                var selection = editor.getSelection();
                var element = selection.getStartElement();

                if (element)
                    element = element.getAscendant('iframe', true);

                if (!element || element.getName() != 'iframe') {
                    element = editor.document.createElement('iframe');
                    this.insertMode = true;
                }
                else
                    this.insertMode = false;

                element.setAttribute('id', "doc-" + (new Date()).valueOf());

                this.element = element;
                if (!this.insertMode)
                    this.setupContent(this.element);
            },
            onOk: function () {
                var abbr = this.element;
                console.log(abbr);
                this.commitContent(abbr);

                if (this.insertMode)
                    editor.insertElement(abbr);
            }
        };
    });
})();